package com.qfedu.controller;

import com.qfedu.entity.Cart;
import com.qfedu.entity.Product;
import com.qfedu.entity.User;
import com.qfedu.service.CartService;
import com.qfedu.service.ProductService;
import com.qfedu.service.impl.CartServiceImpl;
import com.qfedu.service.impl.ProductServiceImpl;
import com.qfedu.utils.BaseServlet;
import com.qfedu.utils.Constants;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/cart.do")
public class CartServletDo extends BaseServlet {
    private CartService cartService = new CartServiceImpl();
    private ProductService productService = new ProductServiceImpl();

    /**
     *
     * @param request
     * @param response
     * @return
     */
    public String create(HttpServletRequest request, HttpServletResponse response){
        //获取uid
        User user = (User) request.getSession().getAttribute("loginUser");
        int uid = user.getUid();
        //获取pid
        String pidStr = request.getParameter("pid");
        int pid = Integer.valueOf(pidStr);

        try {
            //1.根据uid 和 pid 关联查询当前购物车
            Cart product = cartService.getCartById(uid, pid);
            request.setAttribute("list", product);
            //判断是否有购物车
            if (product == null) {
                Product serviceProduct = productService.getProductById(pid);
                BigDecimal ccount = serviceProduct.getPprice();
                int cnum = 1;
                cartService.addCart(uid,pid,cnum,ccount);
            } else {
                Cart cart = (Cart) request.getAttribute("list");
                int cnum = cart.getCnum()+1;
                BigDecimal ccount = cart.getCcount();
                int cid = cart.getCid();
                cartService.update(cnum, ccount, cid);
            }
            request.setAttribute("cartList", product);
            return Constants.FORWARD + "/cartSuccess.jsp";
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        request.setAttribute("msg", "服务器出现异常");
        return Constants.FORWARD + "/message.jsp";
//        User loginUser = (User) request.getSession().getAttribute("loginUser");
//        int uid = loginUser.getUid();
//        int pid = Integer.valueOf(request.getParameter("pid"));
//        try {
//            Cart cart = cartService.getCartById(uid, pid);
//
//            if (cart == null){
//                Cart cart1 = new Cart();
//                cart1.setCnum(1);
//                cartService.addCart(cart1);
//                return Constants.FORWARD + "/cartSuccess.jsp";
//            }
//        } catch (SQLException e) {
//            e.printStackTrace();
//        } catch (IllegalAccessException e) {
//            e.printStackTrace();
//        } catch (InvocationTargetException e) {
//            e.printStackTrace();
//        }
//        request.setAttribute("msg","服务器发生异常");
//        return Constants.FORWARD + "/message.jsp";
//    }

    }
    public String show(HttpServletRequest request, HttpServletResponse response){
        User loginUser = (User) request.getSession().getAttribute("loginUser");
        int uid = loginUser.getUid();
        try {
            List<Cart> list = cartService.getCartListByUid(uid);
            request.setAttribute("list",list);
            return Constants.FORWARD + "/cart.jsp";
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        request.setAttribute("msg","服务器发生异常");
        return Constants.FORWARD + "/message.jsp";
    }
}
